#define _CRT_SECURE_NO_WARNINGS
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* detectCycle(ListNode* head) {
        set<ListNode*> st1;
        ListNode* cur = head;
        while (cur)
        {
            auto it = st1.find(cur);
            if (it == st1.end())
            {
                st1.insert(cur);
            }
            else
            {
                return *it;
            }
            cur = cur->next;
        }
        return nullptr;
    }
};